package com.windoent.yugioh.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.windoent.yugioh.R;

/**
 * author : robert
 * date   : 2021/4/28 11:23
 * desc   :
 */
public class RadioGroupSelectSingleButton extends RelativeLayout {
    private Context mConText;
    private RadioGroup mRadioGroup;
    private RadioButton mRadioButton1;
    private RadioButton mRadioButton2;
    private RadioButton mRadioButton3;

    private String one, two, three;
    private onClickListener listener;
    private boolean showThree;

    public void setOnclickListener(onClickListener clickListener) {
        this.listener = clickListener;
    }

    public RadioGroupSelectSingleButton(Context context) {
        this(context, null, 0);
    }

    public RadioGroupSelectSingleButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadioGroupSelectSingleButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mConText = context;
        LayoutInflater.from(mConText).inflate(R.layout.custom_view_radio_gruop_single, this);
        TypedArray attributes = mConText.obtainStyledAttributes(attrs, R.styleable.RadioGroupSelectSingleButton, 0, 0);
        try {
            showThree = attributes.getBoolean(R.styleable.RadioGroupSelectSingleButton_show_three, false);
            one = attributes.getString(R.styleable.RadioGroupSelectSingleButton_button_one_str);
            two = attributes.getString(R.styleable.RadioGroupSelectSingleButton_button_two_str);
            three = attributes.getString(R.styleable.RadioGroupSelectSingleButton_button_three_str);
        } finally {
            attributes.recycle();
        }
        setUpView();
    }

    private void setUpView() {


        mRadioGroup = (RadioGroup) findViewById(R.id.radio_group);
        mRadioButton1 = (RadioButton) findViewById(R.id.radio_button1);
        mRadioButton2 = (RadioButton) findViewById(R.id.radio_button2);
        mRadioButton3 = (RadioButton) findViewById(R.id.radio_button3);

        mRadioButton3.setVisibility(showThree ? VISIBLE : INVISIBLE);
        mRadioButton1.setText(one);
        mRadioButton2.setText(two);
        mRadioButton3.setText(three);

        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
                int position = -1;
                switch (checkedId) {
                    case R.id.radio_button1:
                        position = 0;
                        break;
                    case R.id.radio_button2:
                        position = 1;
                        break;
                    case R.id.radio_button3:
                        position = 2;
                        break;
                }
                if (listener != null) {
                    listener.onClick(position);
                }

            }
        });

    }

    public int getChecked() {
        if (mRadioButton1.isChecked()) {
            return 0;
        }
        if (mRadioButton2.isChecked()) {
            return 1;
        }
        if (mRadioButton3.isChecked()) {
            return 2;
        }
        return -1;
    }

    public void setOnLyJiFentaoTaiRadio(boolean showThree) {
        mRadioButton1.setVisibility(!showThree ? VISIBLE : GONE);
        mRadioButton2.setVisibility(!showThree ? VISIBLE : GONE);
        mRadioButton3.setVisibility(VISIBLE);
        mRadioButton3.setChecked(showThree);
        mRadioButton1.setChecked(!showThree);
        mRadioButton2.setChecked(false);
    }

    public interface onClickListener {
        public void onClick(int position);
    }

}
